package com.scalar.chiptrack.externalusers.forms;

import com.scalar.chiptrack.ChipTrackActionForm;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.FunctionAccess;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Calendar;


/**
 * Created by IntelliJ IDEA.
 * User: Krishna Prasad
 * Date: Jun 29, 2004
 * Time: 4:08:43 PM
 * To change this template use Options | File Templates.
 */
public class ExternalUserSetupForm extends ChipTrackActionForm
{
    private User externalUser = null;
    private String externalUserId = null;
    private String password = null;
    private String reEnterPassword = null;
    private String firstName = null;
    private String lastName = null;
    private String phoneNumber = null;
    private String faxNumber = null;
    private String email = null;
    private String expiresOn = null;

    private String searchFirstName = null;
    private String searchLastName = null;
    private String userAction = null;

    private int m_vendorId = -1;
    private int userType;
    private boolean disabled = false;
    private boolean expired =false;
    private String sameAsUserId = null;
    public ExternalUserSetupForm()
    {
    }

    public User getExternalUser()
    {
        return externalUser;
    }

    public void setExternalUser( User externalUser )
    {
        this.externalUser = externalUser;
    }

    public String getExternalUserId()
    {
        return externalUserId;
    }

    public void setExternalUserId( String externalUserId )
    {
        this.externalUserId = externalUserId;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword( String password )
    {
        this.password = password;
    }

    public String getReEnterPassword()
    {
        return reEnterPassword;
    }

    public void setReEnterPassword( String reEnterPassword )
    {
        this.reEnterPassword = reEnterPassword;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName( String firstName )
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName( String lastName )
    {
        this.lastName = lastName;
    }

    public String getPhoneNumber()
    {
        return phoneNumber;
    }

    public void setPhoneNumber( String phoneNumber )
    {
        this.phoneNumber = phoneNumber;
    }

    public String getFaxNumber()
    {
        return faxNumber;
    }

    public void setFaxNumber( String faxNumber )
    {
        this.faxNumber = faxNumber;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail( String email )
    {
        this.email = email;
    }

    public String getExpiresOn()
    {
        return expiresOn;
    }

    public void setExpiresOn( String expiresOn )
    {
        this.expiresOn = expiresOn;
    }

    public String getSearchFirstName()
    {
        return searchFirstName;
    }

    public void setSearchFirstName( String searchFirstName )
    {
        this.searchFirstName = searchFirstName;
    }

    public String getSearchLastName()
    {
        return searchLastName;
    }

    public void setSearchLastName( String searchLastName )
    {
        this.searchLastName = searchLastName;
    }

    public String getUserAction()
    {
        return userAction;
    }

    public void setUserAction( String userAction )
    {
        this.userAction = userAction;
    }

    public int getVendorId()
    {
        return m_vendorId;
    }

    public void setVendorId( int vendorId )
    {
        this.m_vendorId = vendorId;
    }

    public int getUserType()
    {
        return userType;
    }

    public void setUserType(int userType)
    {
        this.userType = userType;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

/*
    public boolean isQuoteAckMail() {
        return quoteAckMail;
    }

    public void setQuoteAckMail(boolean quoteAckMail) {
        this.quoteAckMail = quoteAckMail;
    }
*/

    public void reset( ActionMapping mapping, HttpServletRequest request )
    {
        if(externalUser != null)
        {
            externalUser.setQuoteAckMail(false);
        }
        String action = request.getParameter( "action" );
        if( ( "save".equals( action ) ) || ( "update".equals( action )  ) )
        {
            ArrayList functionAccessList = externalUser.getFunctionAccessList();
            Iterator functionAccessIterator = functionAccessList.iterator();
            FunctionAccess functionAccess = null;

            while( functionAccessIterator.hasNext() )
            {
                functionAccess = ( FunctionAccess )functionAccessIterator.next();
                functionAccess.setView( false );
                functionAccess.setModify( false );
                functionAccess.setDelete( false );
            }
        }

    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append( "\n" );
        sb.append( "[External User Id :  " + externalUserId + " ]\n" );
        sb.append( "[Password :  " + password + " ]\n" );
        sb.append( "[RePassword :  " + reEnterPassword + " ]\n" );
        sb.append( "[First Name :  " + firstName + " ]\n" );
        sb.append( "[Last Name :  " + lastName + " ]\n" );
        sb.append( "[Phone Number : " + phoneNumber + " ]\n" );
        sb.append( "[Fax Number : " + faxNumber + " ]\n" );
        sb.append( "[Email : " + email + " ]\n" );
        sb.append( "[Expires On : " + expiresOn + " ]\n" );
        sb.append( "[Srh F Name : " + searchFirstName + " ]\n" );
        sb.append( "[Srh l Name : " + searchLastName + " ]\n" );
        sb.append( "[userAction : " + userAction + " ]\n" );
        // sb.append("[userAction : "+vendorId+" ]\n");
        sb.append( "\n" );

        return sb.toString();

    }

    public boolean isExpired() {

        if (expiresOn == null) return true;

        boolean expired = true;
        try {
            StringTokenizer st = new StringTokenizer (expiresOn, "/");

            int month = Integer.parseInt (st.nextToken());
            int day = Integer.parseInt (st.nextToken());
            int year = Integer.parseInt (st.nextToken());

            Calendar cal = Calendar.getInstance();
            int thisMonth = cal.get (Calendar.MONTH) + 1;
            int today = cal.get (Calendar.DAY_OF_MONTH);
            int thisYear = cal.get (Calendar.YEAR);

            if (year < thisYear) expired = true;
            else if ((month < thisMonth) && (year <= thisYear)) expired = true;
            else if ((day < today) && (month <= thisMonth) && (year <= thisYear)) expired = true;
            else expired = false;
        }
        catch (Exception e) {
            System.out.println ("Exception in user.isExpired (" + expiresOn + ") -- " + e);
        }

        return expired;
    }


    public String getSameAsUserId() {
        return sameAsUserId;
    }

    public void setSameAsUserId(String sameAsUserId) {
        this.sameAsUserId = sameAsUserId;
    }
}
